Skip to content

Conversation

@atournayre
Copy link
Owner

@atournayre atournayre commented Jul 14, 2025

Summary

This PR refactors the Database class to use Symfony's Command/Handler pattern instead of directly using Doctrine's EntityManager. This architectural change improves separation of concerns and testability while maintaining full backward compatibility.

Key Changes

  • Database Class Refactoring: Replace EntityManagerInterface with CommandBusInterface
  • New Command Messages:
    • DatabasePersistCommand for persist operations
    • DatabaseRemoveCommand for remove operations
    • DatabaseFlushCommand for flush operations
  • Command Handlers: Corresponding handlers that encapsulate Doctrine operations
  • SyncCommandInterface: New interface for synchronous command execution
  • Handler Interfaces: Following Elegant Object principles
  • Documentation Updates: Updated database.md and domain-events.md
  • Test Fixes: Updated unit tests to work with command-based architecture

Benefits

  • Better Architecture: Separation of concerns through command/handler pattern
  • Improved Testability: Commands can be easily mocked and tested
  • Future Extensibility: Easy to add middleware, events, or additional logic
  • Backward Compatibility: Public API remains unchanged
  • Code Quality: All tests pass, PHPStan clean

Testing

  • ✅ All 735 tests pass (1275 assertions)
  • ✅ PHPStan analysis passes without errors
  • ✅ Existing API contracts maintained
  • ✅ Handler interfaces follow Elegant Object principles

Test plan

  • Verify all existing Database functionality works as expected
  • Test command dispatching and handler execution
  • Validate backward compatibility with existing code
  • Confirm PHPStan and code quality standards are met

🤖 Generated with Claude Code

atournayre and others added 2 commits July 14, 2025 19:06
…ityManager

- Replace EntityManagerInterface with CommandBusInterface in Database class
- Add SyncCommandInterface for synchronous command execution
- Create DatabasePersistCommand, DatabaseRemoveCommand, and DatabaseFlushCommand
- Add corresponding handlers: DatabasePersistHandler, DatabaseRemoveHandler, DatabaseFlushHandler
- Update Database methods to dispatch commands instead of direct EntityManager calls
- Add handler interfaces following Elegant Object principles
- Update PHPStan configuration to allow Handler suffix in persistence layer
- Update documentation for database.md and domain-events.md
- Fix unit tests to work with new command-based architecture

This refactoring improves architecture by:
- Separating concerns through command/handler pattern
- Enabling better testability and mocking
- Supporting future middleware and event handling
- Maintaining backward compatibility of public API

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Apply Rector formatting corrections
- Add missing semicolons on chained method calls
- Fix parameter alignment in method signatures
- Add missing newlines at end of files

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@atournayre atournayre added this to the 2.13.0 milestone Jul 16, 2025
- Updated `DatabaseTrait::database()` to use `CommandBus` from dependency injection.
- Adjusted `Database::new` instantiation to pass `CommandBus` instead of `EntityManager`.
…andling

- Introduced `EntityDependencyInjection` type hint in `database()` method for clarity.
- Refined assertions to ensure proper dependency injection availability.
- Adjusted `Database::new` instantiation to use `commandBus()` method for compatibility.
@atournayre atournayre merged commit 2e8d3c5 into main Aug 6, 2025
8 checks passed
@atournayre atournayre deleted the feature/database-command-messages branch August 6, 2025 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants